package com.dragon.mmochat.service;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.MmochatMain;
import com.dragon.mmochat.message.Mmochat_11_2S;
import com.dragon.mmochat.message.Mmochat_15_10S;
import com.dragon.mmochat.message.Mmochat_15_11C;
import com.dragon.mmochat.message.Mmochat_15_12S;
import com.dragon.mmochat.message.Mmochat_15_13C;
import com.dragon.mmochat.message.Mmochat_15_14C;
import com.dragon.mmochat.message.Mmochat_15_15C;
import com.dragon.mmochat.message.Mmochat_15_16C;
import com.dragon.mmochat.message.Mmochat_15_17C;
import com.dragon.mmochat.message.Mmochat_15_18C;
import com.dragon.mmochat.message.Mmochat_15_19C;
import com.dragon.mmochat.message.Mmochat_15_1C;
import com.dragon.mmochat.message.Mmochat_15_20C;
import com.dragon.mmochat.message.Mmochat_15_21C;
import com.dragon.mmochat.message.Mmochat_15_22C;
import com.dragon.mmochat.message.Mmochat_15_23C;
import com.dragon.mmochat.message.Mmochat_15_2S;
import com.dragon.mmochat.message.Mmochat_15_3C;
import com.dragon.mmochat.message.Mmochat_15_4C;
import com.dragon.mmochat.message.Mmochat_15_4S;
import com.dragon.mmochat.message.Mmochat_15_5C;
import com.dragon.mmochat.message.Mmochat_15_6C;
import com.dragon.mmochat.message.Mmochat_15_7C;
import com.dragon.mmochat.message.Mmochat_15_8C;
import com.dragon.mmochat.message.Mmochat_15_9C;
import com.dragon.mmochat.message.Mmochat_4_12S;
import com.dragon.mmochat.message.Mmochat_4_8S;
import com.dragon.mmochat.model.MmochatChenHao;
import com.dragon.mmochat.model.MmochatTeam;
import com.dragon.mmochat.model.enumType.MmochatBattleOrderType;
import com.dragon.mmochat.model.enumType.MmochatChatChannelType;
import com.dragon.mmochat.model.enumType.MmochatChenHaoType;
import com.dragon.mmochat.model.enumType.MmochatGangLevelType;
import com.dragon.mmochat.model.enumType.MmochatGangMemberType;
import com.dragon.mmochat.model.enumType.MmochatGangSkillType;
import com.dragon.mmochat.model.enumType.MmochatNpcFaceType;
import com.dragon.mmochat.model.enumType.MmochatNpcItemType;
import com.dragon.mmochat.model.enumType.MmochatRoleBmpType;
import com.dragon.mmochat.model.enumType.MmochatServerType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyCostType;
import com.dragon.mmochat.model.enumType.MmochatSmallMoneyEarnType;
import com.dragon.mmochat.model.enumType.MmochatTaskStateType;
import com.dragon.mmochat.model.enumType.MmochatTaskType;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.gang.MmochatGangMember;
import com.dragon.mmochat.model.gang.MmochatGangRequester;
import com.dragon.mmochat.model.gang.MmochatGangSkill;
import com.dragon.mmochat.model.map.MmochatMap;
import com.dragon.mmochat.model.map.MmochatMapTransPoint;
import com.dragon.mmochat.model.mergeServer.MergeServer;
import com.dragon.mmochat.model.role.MmochatNpc;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.dragon.mmochat.model.scheduleJob.pkGame.activities.MmochatGangMixedPkGame;
import com.dragon.mmochat.model.task.MmochatTask;
import com.dragon.mmochat.service.MmochatShopService.RankShop;
import com.dragon.mmochat.util.CreateTlvWin;
import com.dragon.mmochat.util.MmochatUtil;
import com.skymobi.handler.SkymobiHandler;
import com.skymobi.message.SkymobiProtocolMessage;
import com.skymobi.message.widget.AddDataTLV;
import com.skymobi.message.widget.ClearDataTLV;
import com.skymobi.message.widget.CloseWinTLV;
import com.skymobi.message.widget.CreateMsgBoxTLV;
import com.skymobi.message.widget.SendDataTLV;
import com.skymobi.message.widget.ShowWaitingMsgBoxTLV;
import com.skymobi.message.widget.WinType;
import com.skymobi.service.SkymobiService;
import com.skymobi.util.Text;

//帮派
public class MmochatGangService {
	private static Logger log = LoggerFactory
			.getLogger(MmochatGangService.class);

	public static ScheduledExecutorService checkGangThread = Executors
			.newScheduledThreadPool(1);

	public static Map<Integer, MmochatGang> gangs = new ConcurrentHashMap<Integer, MmochatGang>();

	@SuppressWarnings("deprecation")
	public void init() {
		// 读取所有帮派,getAllGang
		if (!MmochatMain.openMergeServer) {
			try {
				long t = System.currentTimeMillis();
				List<MmochatGang> gangList = MmochatDao.getAllGang();
				for (MmochatGang gang : gangList) {
					gangs.put(gang.getId(), gang);
				}
				System.out.println("加载所有帮派成功!耗时:"
						+ (System.currentTimeMillis() - t) + "ms");
			} catch (Exception e) {
				e.printStackTrace();
			}

			// 每天7:00进行检查帮派
			Date now = new Date();
			int hour = now.getHours();
			int minutes = now.getMinutes();
			int sec = now.getSeconds();
			int startTime = (int) ((60 - sec) + (60 - minutes - 1)
					* MmochatConstant.SECOND_PER_MIN + (7 - hour - 1)
					* MmochatConstant.SECOND_PER_HOUR);
			if (startTime <= 0) {
				startTime += MmochatConstant.SECOND_PER_DAY;
			}
			checkGangThread.scheduleAtFixedRate(new checkGang(), startTime,
					MmochatConstant.SECOND_PER_DAY, TimeUnit.SECONDS);

		} else {
			if (MmochatMain.mergeServerStep == 2) {
				try {
					long t = System.currentTimeMillis();
					List<MmochatGang> gangList = MmochatDao
							.getAllGangOfMergeServer();
					for (MmochatGang gang : gangList) {
						gangs.put(gang.getId(), gang);
					}
					System.out.println("加载所有合服帮派成功!耗时:"
							+ (System.currentTimeMillis() - t) + "ms");

				} catch (Exception e) {
					e.printStackTrace();
				}
				// 合服才能用
				MergeServer server = new MergeServer();
				server.run();
			}
		}
	}

	// 帮派检查：帮主删号或帮主连续30天未登陆，则由副帮主接位，
	// 若副帮主已删号或连续5天未登陆，则由5天内登陆过的成员中帮贡最高的接位
	class checkGang implements Runnable {
		@Override
		public void run() {
			try {
				if (MmochatMain.serverType == MmochatServerType.游戏服务器) {
					log.debug("检测帮主是否久未上线!");
					for (MmochatGang gang : gangs.values()) {
						try {
							for (MmochatGangMember member : gang.getMembers()
									.values()) {
								if (member.getType() == MmochatGangMemberType.帮主) {
									if (member.getCurWeekBuildValue() > 0) {
										// 本周有贡献值，肯定上过线
										break;
									} else {
										MmochatPlayer leader = MmochatGmService
												.getRoleInfoById(member
														.getRoleId());
										if (leader == null) {
											break;
										}
										if (leader.isDeleted()
												|| (System.currentTimeMillis()
														- leader
																.getLatest_login_time() > MmochatConstant.MS_PER_DAY * 14)) {
											// 已删号或14天未登陆
											List<MmochatGangMember> memberList = new ArrayList<MmochatGangMember>(
													gang.getMembers().values());
											Collections
													.sort(
															memberList,
															new Comparator<MmochatGangMember>() {
																@Override
																public int compare(
																		MmochatGangMember m1,
																		MmochatGangMember m2) {
																	return m2
																			.getBuildValue()
																			- m1
																					.getBuildValue();
																}
															});
											// 给副帮主
											MmochatGangMember newLeader = null;
											for (MmochatGangMember he : memberList) {
												if (he.getType() == MmochatGangMemberType.副帮) {
													if (he
															.getCurWeekBuildValue() > 0) {
														// 本周有贡献值，肯定上过线
														newLeader = he;
														break;
													} else {
														MmochatPlayer hisRole = MmochatGmService
																.getRoleInfoById(he
																		.getRoleId());
														if (!hisRole
																.isDeleted()
																&& (System
																		.currentTimeMillis()
																		- hisRole
																				.getLatest_login_time() < MmochatConstant.MS_PER_DAY * 5)) {
															newLeader = he;
															break;
														}
													}
												}
											}
											if (newLeader == null) {
												// 副帮主不满足条件，给5天内上线过的帮贡最高成员
												for (MmochatGangMember he : memberList) {
													if (he
															.getCurWeekBuildValue() > 0) {
														// 本周有贡献值，肯定上过线
														newLeader = he;
														break;
													} else {
														MmochatPlayer hisRole = MmochatGmService
																.getRoleInfoById(he
																		.getRoleId());
														if (!hisRole
																.isDeleted()
																&& (System
																		.currentTimeMillis()
																		- hisRole
																				.getLatest_login_time() < MmochatConstant.MS_PER_DAY * 5)) {
															newLeader = he;
															break;
														}
													}
												}
											}

											if (newLeader != null) {
												// 转移帮主
												MmochatGangMember hisInfo = newLeader;
												MmochatGangMember myInfo = member;
												// 去除旧帮主，设置新帮主，帮主更替后向所有在线帮众群发消息
												MmochatGangMemberType oldType = hisInfo
														.getType();
												hisInfo
														.setType(MmochatGangMemberType.帮主);
												myInfo
														.setType(MmochatGangMemberType.帮众);
												try {
													MmochatDao
															.updateGangMember(gang);
												} catch (Exception e) {
													e.printStackTrace();
													hisInfo.setType(oldType);
													myInfo
															.setType(MmochatGangMemberType.帮主);
													break;
												}

												// 称号
												MmochatChenHao
														.delAndUpdateChenHaoAndSaveSql(
																member
																		.getRoleId(),
																MmochatChenHaoType.帮主);
												MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
														.getGangChenhaoList();
												for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
													MmochatChenHao
															.delAndUpdateChenHaoAndSaveSql(
																	hisInfo
																			.getRoleId(),
																	chenhaoType);
												}
												MmochatChenHao chenhao = new MmochatChenHao();
												chenhao
														.setType(MmochatChenHaoType.帮众);
												chenhao
														.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*1%");
												chenhao.setName(gang.getName()
														+ " "
														+ MmochatChenHaoType.帮众
																.toString());
												MmochatChenHao
														.addAndUpdateChenHaoAndSaveSql(
																member
																		.getRoleId(),
																chenhao);

												chenhao = new MmochatChenHao();
												chenhao
														.setType(MmochatChenHaoType.帮主);
												chenhao
														.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*2%");
												chenhao.setName(gang.getName()
														+ " "
														+ MmochatChenHaoType.帮主
																.toString());
												MmochatChenHao
														.addAndUpdateChenHaoAndSaveSql(
																hisInfo
																		.getRoleId(),
																chenhao);

												sendMsgToAllGangMembersByFriendMsg(
														gang,
														MmochatUtil
																.wrapColor(
																		"系统消息:由于帮主<"
																				+ member
																						.getName()
																				+ ">连续14天未上线，帮内不能一日无主，系统已将"
																				+ MmochatUtil
																						.wrapColor(
																								"帮主之位",
																								Color.red)
																				+ "传与"
																				+ MmochatUtil
																						.wrapColor(
																								hisInfo
																										.getName(),
																								Color.green)
																				+ "!(优先由5天内登陆过的副帮主接位，其次考虑5天内登陆过的帮贡最高的成员接位)",
																		Color.yellow));
												String content = "由于帮主连续14天未上线，帮内不能一日无主，"
														+ "根据职位、帮贡和在线情况，您是最佳帮主继位人选，"
														+ MmochatUtil
																.wrapColor(
																		"现系统指任您为本帮帮主",
																		Color.red)
														+ "，望带领本帮闯出一番天地!";
												MmochatGmService
														.sendKufuPrivateMsg(
																newLeader
																		.getRoleId(),
																content, false);

												MmochatGmService
														.sendKufuPrivateMsg(
																member
																		.getRoleId(),
																"由于您连续14天以上未登陆游戏，系统已将帮主之位交给<"
																		+ newLeader
																				.getName()
																		+ ">!",
																false);

												try {
													Thread.sleep(1000);
												} catch (InterruptedException e) {
													e.printStackTrace();
												}
											}
										}
										break;
									}
								}
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	// 根据ID获取帮派
	@SuppressWarnings("deprecation")
	public static MmochatGang getGangById(int id) {
		MmochatGang gang = gangs.get(id);
		if (gang == null) {
			try {
				gang = MmochatDao.getGangById(id);
				if (gang != null) {
					gangs.put(gang.getId(), gang);
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		if (gang == null) {
			return null;
		}
		// 判断是否需要清空成员每周帮贡值
		Date now = new Date();
		Date last = gang.getLastDayClearMembersWeekBuildValue();
		boolean needClear = false;
		if (last.getTime() == 0) {
			needClear = true;
		} else {
			// 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 =
			// Thursday, 5 = Friday, 6 = Saturday
			int hour = last.getHours();
			int minutes = last.getMinutes();
			int sec = last.getSeconds();
			int day = last.getDay();
			if (day == 0) {
				day = 7;
			}
			long nextClearInterval = (60 - sec) + (60 - minutes - 1)
					* MmochatConstant.SECOND_PER_MIN + (24 - hour - 1)
					* MmochatConstant.SECOND_PER_HOUR + (7 - day)
					* MmochatConstant.SECOND_PER_DAY;
			nextClearInterval *= MmochatConstant.MS_PER_SECOND;
			if (now.getTime() - last.getTime() >= nextClearInterval) {
				needClear = true;
			}
		}
		// 重置每周帮贡
		if (needClear) {
			for (MmochatGangMember member : gang.getMembers().values()) {
				member.setCurWeekBuildValue(0);
			}
			gang.setLastDayClearMembersWeekBuildValue(new Date());
			try {
				MmochatDao.resetGangMemberWeekBuildValue(gang);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return gang;
	}

	// 获取所有帮派
	public static List<MmochatGang> getAllGang() {
		return new ArrayList<MmochatGang>(gangs.values());
	}

	// 获取帮派地图
	public static MmochatMap getGangMap(MmochatGang gang) {
		MmochatMap gangMap = null, roomAMap = null, roomBMap = null;
		if (gang == null) {
			return null;
		}
		gangMap = MmochatMapService.getMapByMapId(gang.getMapId());
		if (gangMap == null) {
			// 地图尚未加载，根据帮派信息创建帮派地图
			// 房间A地图
			MmochatMap map = new MmochatMap();
			roomAMap = map;
			map.setMapId(gang.getRoomA_mapId());
			map.setMapDataId(MmochatConstant.mapDataId_gangRoomA);
			map.setWalkToBattle(false);
			map.setMapName("帮派总堂");
			map.setCanTransToFriend(false);
			MmochatMapTransPoint transPoint = new MmochatMapTransPoint();
			transPoint.setX(286);
			transPoint.setY(217);
			transPoint.setTransToMapId(gang.getMapId());
			transPoint.setNewMapX(93);
			transPoint.setNewMapY(139);
			map.getTransPoints().add(transPoint);

			MmochatMapService.maps.put(map.getMapId(), map);

			// 帮派总管
			MmochatNpc npc = new MmochatNpc();
			npc.setNpcId(MmochatConstant.npcId_gangNpc2Base + gang.getId());
			npc.setMapId(roomAMap.getMapId());
			npc.setX((short) 339);
			npc.setY((short) 110);
			npc.setName("帮派总管");
			npc.setBmpType(MmochatRoleBmpType.老头儿);
			npc.setFace(MmochatNpcFaceType.朝左);
			npc.getFunctionItems().add(MmochatNpcItemType.为帮派出力);
			npc.getFunctionItems().add(MmochatNpcItemType.更换帮派任务);
			npc.getFunctionItems().add(MmochatNpcItemType.放弃任务);
			roomAMap.addNpc(npc);
			MmochatNpcService.npcs.put(npc.getNpcId(), npc);

			// 将所有交任务NPC为帮派总管的任务加到此NPC身上(接任务NPC的情况不加)
			for (MmochatTask task : MmochatTaskService.tasks.values()) {
				if (task.getFinish_npcId() == MmochatConstant.npcId_gangNpc2_inFile) {
					npc.addTask(task);
				}
			}

			// 房间B地图
			map = new MmochatMap();
			roomBMap = map;
			map.setMapId(gang.getRoomB_mapId());
			map.setMapDataId(MmochatConstant.mapDataId_gangRoomB);
			map.setWalkToBattle(false);
			map.setMapName("帮派客厅");
			map.setCanTransToFriend(false);
			transPoint = new MmochatMapTransPoint();
			transPoint.setX(146);
			transPoint.setY(217);
			transPoint.setTransToMapId(gang.getMapId());
			transPoint.setNewMapX(333);
			transPoint.setNewMapY(100);
			map.getTransPoints().add(transPoint);

			MmochatMapService.maps.put(map.getMapId(), map);

			// 帮派军师
			npc = new MmochatNpc();
			npc.setNpcId(MmochatConstant.npcId_gangNpc3Base + gang.getId());
			npc.setMapId(roomBMap.getMapId());
			npc.setX((short) 80);
			npc.setY((short) 102);
			npc.setName("帮派军师");
			npc.setBmpType(MmochatRoleBmpType.姑娘儿);
			npc.setFace(MmochatNpcFaceType.朝右);
			npc.getFunctionItems().add(MmochatNpcItemType.学习帮派技能);
			npc.getFunctionItems().add(MmochatNpcItemType.帮派升级);
			npc.getFunctionItems().add(MmochatNpcItemType.提取帮派资金);
			npc.getFunctionItems().add(MmochatNpcItemType.开启血魔副本);
			npc.getFunctionItems().add(MmochatNpcItemType.血魔副本介绍);
			roomBMap.addNpc(npc);
			MmochatNpcService.npcs.put(npc.getNpcId(), npc);

			map = new MmochatMap();
			gangMap = map;
			map.setMapId(gang.getMapId());
			// TODO:帮派地图，是否分等级设置不同的帮派地图？
			map.setMapDataId(MmochatConstant.mapDataId_gang);
			map.setWalkToBattle(false);
			map.setMapName(gang.getName());
			map.setCanTransToFriend(false);

			// 指令
			map.getBattleOrders().add(MmochatBattleOrderType.隐身);
			map.getBattleOrders().add(MmochatBattleOrderType.引怪);

			// NPC及传送口设置
			transPoint = new MmochatMapTransPoint();
			transPoint.setX(61);
			transPoint.setY(109);
			transPoint.setTransToMapId(gang.getRoomA_mapId());
			transPoint.setNewMapX(243);
			transPoint.setNewMapY(192);
			map.getTransPoints().add(transPoint);

			transPoint = new MmochatMapTransPoint();
			transPoint.setX(373);
			transPoint.setY(61);
			transPoint.setTransToMapId(gang.getRoomB_mapId());
			transPoint.setNewMapX(193);
			transPoint.setNewMapY(194);
			map.getTransPoints().add(transPoint);

			npc = new MmochatNpc();
			npc.setNpcId(MmochatConstant.npcId_gangNpc1Base + gang.getId());
			npc.setMapId(gangMap.getMapId());
			npc.setX((short) 220);
			npc.setY((short) 68);
			npc.setName("帮派传送人");
			npc.setBmpType(MmochatRoleBmpType.乞丐);
			npc.setFace(MmochatNpcFaceType.朝左);
			npc.getFunctionItems().add(MmochatNpcItemType.前往余杭);
			npc.getFunctionItems().add(MmochatNpcItemType.前往江宁);
			npc.getFunctionItems().add(MmochatNpcItemType.前往开封);
			gangMap.addNpc(npc);
			MmochatNpcService.npcs.put(npc.getNpcId(), npc);

			MmochatMapService.maps.put(map.getMapId(), map);
		}
		return gangMap;
	}

	// 创建帮派
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_1(SkymobiHandler handler,
			Mmochat_15_1C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经有帮派了!");
		}
		if (me.getLevel() < MmochatConstant.minLevelNeedToCreateGang) {
			return MmochatUtil.msgbox(req.getHallState(), "创建帮派需要人物达到"
					+ MmochatConstant.minLevelNeedToCreateGang + "级以上。");
		}
		if (me.getSmall_money() < MmochatConstant.moneyForCreatGang) {
			return MmochatUtil.msgbox(req.getHallState(), "创建帮派需要"
					+ MmochatUtil
							.getColorMoney(MmochatConstant.moneyForCreatGang)
					+ "文,您的金钱不够!");
		}
		String gangName = req.getName();
		if (gangName == null || gangName.length() < 1 || gangName.length() > 6) {
			return MmochatUtil.msgbox(req.getHallState(), "帮派名必需是1到6个字!请重新输入。");
		}

		// 非法字检测
		if (!MmochatMainService.isRoleNameValid(gangName)) {
			return MmochatUtil.msgbox(req.getHallState(), "帮派名中包含非法字词!请重新输入。");
		}

		// 扣除创建帮派的资金
		me.addAndUpdateSmall_money(-1 * MmochatConstant.moneyForCreatGang);

		// 帮派称谓
		MmochatChenHao chenhao = new MmochatChenHao();
		chenhao.setType(MmochatChenHaoType.帮主);
		chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*2%");
		chenhao.setName(gangName + " 帮主");
		me.addAndUpdateChenHao(chenhao);

		// 创建并加入此帮派，并将此帮派加入到gangs中
		try {
			MmochatDao.createGang(me, gangName, gangs);
		} catch (DataIntegrityViolationException e) {
			me.addAndUpdateSmall_money(MmochatConstant.moneyForCreatGang);
			me.delAndUpdateChenHao(MmochatChenHaoType.帮主);
			return MmochatUtil.msgbox(req.getHallState(), "该帮派名已存在!");
		} catch (Exception e) {
			e.printStackTrace();
			me.delAndUpdateChenHao(MmochatChenHaoType.帮主);
			me.addAndUpdateSmall_money(MmochatConstant.moneyForCreatGang);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		MmochatStatisticsService.addSmallMoneyCost(
				MmochatSmallMoneyCostType.创建帮派, -1
						* MmochatConstant.moneyForCreatGang);
		String sysInfo = MmochatUtil.wrapColor(me.getName(), Color.green)
				+ "创建了新的帮派<" + MmochatUtil.wrapColor(gangName, Color.yellow)
				+ ">，欢迎各位英雄加盟!";
		MmochatChatService.sendSystemMsg(sysInfo);
		return MmochatUtil.msgbox(req.getHallState(), "创建成功!请按8键查看帮派。");
	}

	// 修改帮派公告
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_3(SkymobiHandler handler,
			Mmochat_15_3C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您不在此帮派!");
		}
		if (myInfo.getType() != MmochatGangMemberType.帮主
				&& myInfo.getType() != MmochatGangMemberType.副帮) {
			return MmochatUtil.msgbox(req.getHallState(), "您没有权限修改公告!");
		}
		String newPubInfo = req.getNewPubInfo();
		if (newPubInfo.contains("") || newPubInfo.contains("")
				|| newPubInfo.contains("") || newPubInfo.contains("")) {
			return MmochatUtil.msgbox(req.getHallState(), "您的公告内容中包含不可见字符!");
		}
		if (newPubInfo.length() > 98) {
			return MmochatUtil.msgbox(req.getHallState(), "公告最多98个字!");
		}
		try {
			MmochatDao.updateGangInfo(newPubInfo, myGang);
		} catch (Exception e) {
			e.printStackTrace();
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		Mmochat_15_2S pack = new Mmochat_15_2S();
		pack.setHallState(req.getHallState());
		pack.setGang(myGang);
		pack.setMyType(myInfo.getType());
		return pack;
	}

	// 查看指定页的帮派成员列表
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_4(SkymobiHandler handler,
			Mmochat_15_4C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		Mmochat_15_4S pack = new Mmochat_15_4S();
		pack.setHallState(req.getHallState());
		pack.setGang(myGang);
		pack.setCurPage(req.getPage());
		return pack;
	}

	// 向所有在线帮众群发好友消息
	public static void sendMsgToAllGangMembersByFriendMsg(MmochatGang gang,
			String msg) {
		if (gang == null || msg == null) {
			return;
		}
		for (MmochatGangMember member : gang.getMembers().values()) {
			MmochatPlayer role = MmochatMainService.players.get(member
					.getRoleId());
			if (role != null) {
				// 以好友消息的方式向帮众下发帮派消息
				MmochatFriendService.sendMsgToPersonByFriendMessage(role, msg);
			}
		}
	}

	// 系统向所有在线帮众群发帮派频道聊天消息
	public static void sendGangSystemMsg(MmochatGang gang, String context) {
		if (context == null || context.length() == 0 || gang == null) {
			return;
		}
		Mmochat_11_2S pack = new Mmochat_11_2S();
		pack.setType(MmochatChatChannelType.帮派.getValue());
		pack.setRoleId(-1);
		pack.setEquipId(-1);

		String wholeScreenMsg = MmochatUtil
				.wrapIcon(MmochatConstant.BMP_CHAT_GANGTAG);
		wholeScreenMsg += MmochatUtil.wrapColor(context, new Color(250, 150,
				249));

		// 向所有玩家发送聊天
		for (MmochatGangMember member : gang.getMembers().values()) {
			MmochatPlayer p = MmochatMainService.players
					.get(member.getRoleId());
			if (p != null) {
				if (p.getShowChatSet().get(MmochatChatChannelType.帮派)) {
					pack.setLine((byte) p.getLine());
					pack.setMsg(wholeScreenMsg);
					MmochatUtil.sendCommonPack(p, pack);
				}
			}
		}
	}

	// 向帮众群发消息
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_5(SkymobiHandler handler,
			Mmochat_15_5C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您不在此帮派!");
		}
		if (myInfo.getType() != MmochatGangMemberType.帮主
				&& myInfo.getType() != MmochatGangMemberType.副帮) {
			return MmochatUtil.msgbox(req.getHallState(), "您没有权限群发消息!");
		}
		if (!myGang.canSendMsgToAll()) {
			return MmochatUtil.msgbox(req.getHallState(), "每隔"
					+ MmochatConstant.intervalOfGangSendingMsgToAll
					/ MmochatConstant.MS_PER_MINUTE + "分钟才能群发一次消息,请不要频繁群发消息。");
		}
		myGang.updateSendMsgToAllTick();
		String msg = MmochatUtil.wrapColor("帮派群发[" + me.getName() + "]:",
				Color.yellow)
				+ MmochatFilterService.getFilterString(req.getMsg());
		sendMsgToAllGangMembersByFriendMsg(myGang, msg);

		return null;
	}

	// 踢出帮派
	@SuppressWarnings("unchecked")
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_6(SkymobiHandler handler,
			Mmochat_15_6C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您不在此帮派!");
		}
		if (myInfo.getType() != MmochatGangMemberType.帮主
				&& myInfo.getType() != MmochatGangMemberType.副帮) {
			return MmochatUtil.msgbox(req.getHallState(), "您没有权限踢人!");
		}
		MmochatGangMember hisInfo = myGang.getMembers().get(
				req.getTargetRoleId());
		if (hisInfo != null) {
			if (req.getTargetRoleId() == me.getRoleId()) {
				return MmochatUtil.msgbox(req.getHallState(), "您不能踢自己!");
			}
			if (hisInfo.getType().getValue() >= myInfo.getType().getValue()) {
				return MmochatUtil.msgbox(req.getHallState(), "您没有足够权限踢除对方!");
			}

			boolean online = true;
			Map<Integer, MmochatTask> taskBak = null; // 内存中的任务
			MmochatPlayer he = MmochatMainService.getRoleFromBuffer(req
					.getTargetRoleId());
			if (he == null) {
				online = false;
				try {
					he = MmochatDao.getRole(req.getTargetRoleId());
				} catch (EmptyResultDataAccessException e) {
					// 此玩家已经删号
					he = null;
				} catch (Exception e) {
					e.printStackTrace();
					return MmochatUtil
							.msgbox(req.getHallState(), "服务器忙，请稍候再试!");
				}
			} else {
				taskBak = (Map<Integer, MmochatTask>) MmochatUtil.deepClone(he
						.getTasks());
			}

			if (he != null) {
				// 清除帮派任务
				MmochatTask myTask = he
						.getUnFinishedTaskByType(MmochatTaskType.帮派任务);
				if (myTask != null
						&& (myTask.getTaskState() == MmochatTaskStateType.已接 || myTask
								.getTaskState() == MmochatTaskStateType.待完成)) {
					he.getTasks().remove(myTask.getTaskId());
					Integer nextTaskId = myTask.getNext_taskId();
					while (nextTaskId != null) {
						MmochatTask task = he.getTaskById(nextTaskId);
						if (task == null) {
							break;
						}
						he.getTasks().remove(nextTaskId);
						nextTaskId = task.getNext_taskId();
					}
					Integer nextAccept_taskId = myTask.getNextAccept_taskId();
					while (nextAccept_taskId != null) {
						MmochatTask task = he.getTaskById(nextAccept_taskId);
						if (task == null) {
							break;
						}
						he.getTasks().remove(nextAccept_taskId);
						nextAccept_taskId = task.getNextAccept_taskId();
					}
				}

				// 去除帮派称谓
				MmochatChenHaoType[] chenhaoList = MmochatChenHaoType
						.getGangChenhaoList();
				for (MmochatChenHaoType type : chenhaoList) {
					he.delAndUpdateChenHao(type);
				}
			}

			// 踢出对方
			myGang.getMembers().remove(req.getTargetRoleId());

			// 保存数据库,更新帮派和被踢者的帮派信息，如果在线，还要更新其内存中的帮派信息
			try {
				MmochatDao.kickOutGangMember(myGang, req.getTargetRoleId(), he);
				if (online && he != null) {
					he.setNoGang();
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							he, "您被请出了帮派:"
									+ MmochatUtil.wrapColor(myGang.getName(),
											Color.green));
				}
				// 向帮众发布消息
				if (he != null) {
					sendGangSystemMsg(myGang, MmochatUtil.wrapColor(he
							.getName(), Color.yellow)
							+ "被" + me.getName() + "请离了帮派。");
				}
			} catch (Exception e) {
				e.printStackTrace();
				// 踢出失败
				myGang.getMembers().put(req.getTargetRoleId(), hisInfo);
				if (he != null && online) {
					// 恢复任务数据
					he.setTasks(taskBak);
				}
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
		}

		// 更新帮众列表
		Mmochat_15_4S pack = new Mmochat_15_4S();
		pack.setHallState(req.getHallState());
		pack.setGang(myGang);
		pack.setCurPage(req.getPage());
		return pack;
	}

	// 设置帮派职位
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_7(SkymobiHandler handler,
			Mmochat_15_7C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您不在此帮派!");
		}
		if (myInfo.getType() != MmochatGangMemberType.帮主) {
			return MmochatUtil.msgbox(req.getHallState(), "您没有权限设置帮众的职位!");
		}
		MmochatGangMember hisInfo = myGang.getMembers().get(
				req.getTargetRoleId());
		if (hisInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "对方已经离开本帮!");
		}
		MmochatGangMemberType memberType = MmochatGangMemberType.get(req
				.getMemberTypeValue());
		if (memberType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无法设置此职位!");
		}
		if (req.getTargetRoleId() == me.getRoleId()) {
			return MmochatUtil.msgbox(req.getHallState(), "您不能对自己进行设置!");
		}
		if (hisInfo.getType() == memberType) {
			return MmochatUtil.msgbox(req.getHallState(), "对方已经是"
					+ memberType.toString() + "!");
		}
		MmochatGangMemberType oldType = hisInfo.getType();
		switch (memberType) {
		case 帮主: {
			// 去除旧帮主，设置新帮主，帮主更替后向所有在线帮众群发消息
			hisInfo.setType(MmochatGangMemberType.帮主);
			myInfo.setType(MmochatGangMemberType.帮众);
			try {
				MmochatDao.updateGangMember(myGang);
			} catch (Exception e) {
				e.printStackTrace();
				hisInfo.setType(oldType);
				myInfo.setType(MmochatGangMemberType.帮主);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}

			// 称号
			MmochatChenHao.delAndUpdateChenHaoAndSaveSql(me.getRoleId(),
					MmochatChenHaoType.帮主);
			MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
					.getGangChenhaoList();
			for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(hisInfo
						.getRoleId(), chenhaoType);
			}
			MmochatChenHao chenhao = new MmochatChenHao();
			chenhao.setType(MmochatChenHaoType.帮众);
			chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*1%");
			chenhao.setName(myGang.getName() + " "
					+ MmochatChenHaoType.帮众.toString());
			MmochatChenHao.addAndUpdateChenHaoAndSaveSql(me.getRoleId(),
					chenhao);

			chenhao = new MmochatChenHao();
			chenhao.setType(MmochatChenHaoType.帮主);
			chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*2%");
			chenhao.setName(myGang.getName() + " "
					+ MmochatChenHaoType.帮主.toString());
			MmochatChenHao.addAndUpdateChenHaoAndSaveSql(hisInfo.getRoleId(),
					chenhao);

			sendMsgToAllGangMembersByFriendMsg(myGang, MmochatUtil.wrapColor(
					"系统消息:" + me.getName() + "已将帮主之位传与" + hisInfo.getName()
							+ "!", Color.yellow));
			break;
		}
		case 副帮: {
			// 如果副帮主名额未满则可设置，否则提示名额已满
			if (myGang.isMemberTypeFull(MmochatGangMemberType.副帮)) {
				return MmochatUtil.msgbox(req.getHallState(), "设置失败,副帮主名额已满!"
						+ myGang.getLevel().toString() + "帮派最多可设置"
						+ myGang.getLevel().getMaxViceMasterNum() + "名副帮主。");
			}
			hisInfo.setType(MmochatGangMemberType.副帮);
			try {
				MmochatDao.updateGangMember(myGang);
			} catch (Exception e) {
				e.printStackTrace();
				hisInfo.setType(oldType);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}

			MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
					.getGangChenhaoList();
			for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(hisInfo
						.getRoleId(), chenhaoType);
			}

			MmochatChenHao chenhao = new MmochatChenHao();
			chenhao.setType(MmochatChenHaoType.副帮主);
			chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*2%");
			chenhao.setName(myGang.getName() + " "
					+ MmochatChenHaoType.副帮主.toString());
			MmochatChenHao.addAndUpdateChenHaoAndSaveSql(hisInfo.getRoleId(),
					chenhao);

			MmochatPlayer he = MmochatMainService.players.get(req
					.getTargetRoleId());
			if (he != null) {
				MmochatFriendService.sendSystemMsgToPersonByFriendMessage(he,
						"您已经被帮主提拔为" + MmochatUtil.wrapColor("副帮主", Color.green)
								+ "了!帮派的发展靠大家，大家一起努力吧!");
			}
			break;
		}
		case 精英: {
			// 如果精英名额未满则可设置，否则提示名额已满
			if (myGang.isMemberTypeFull(MmochatGangMemberType.精英)) {
				return MmochatUtil.msgbox(req.getHallState(), "设置失败,精英名额已满!"
						+ myGang.getLevel().toString() + "帮派最多可设置"
						+ myGang.getLevel().getMaxVipMemberNum() + "名精英。");
			}
			hisInfo.setType(MmochatGangMemberType.精英);
			try {
				MmochatDao.updateGangMember(myGang);
			} catch (Exception e) {
				e.printStackTrace();
				hisInfo.setType(oldType);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
					.getGangChenhaoList();
			for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(hisInfo
						.getRoleId(), chenhaoType);
			}

			MmochatChenHao chenhao = new MmochatChenHao();
			chenhao.setType(MmochatChenHaoType.精英);
			chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*2%");
			chenhao.setName(myGang.getName() + " "
					+ MmochatChenHaoType.精英.toString());
			MmochatChenHao.addAndUpdateChenHaoAndSaveSql(hisInfo.getRoleId(),
					chenhao);

			if (oldType.getValue() < MmochatGangMemberType.精英.getValue()) {
				MmochatPlayer he = MmochatMainService.players.get(req
						.getTargetRoleId());
				if (he != null) {
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							he, "您已经被帮主提拔为"
									+ MmochatUtil.wrapColor("精英", Color.green)
									+ "了!帮派的发展靠大家，大家一起努力吧!");
				}
			}
			break;
		}
		case 帮众: {
			hisInfo.setType(MmochatGangMemberType.帮众);
			try {
				MmochatDao.updateGangMember(myGang);
			} catch (Exception e) {
				e.printStackTrace();
				hisInfo.setType(oldType);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
			MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
					.getGangChenhaoList();
			for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
				MmochatChenHao.delAndUpdateChenHaoAndSaveSql(hisInfo
						.getRoleId(), chenhaoType);
			}

			MmochatChenHao chenhao = new MmochatChenHao();
			chenhao.setType(MmochatChenHaoType.帮众);
			chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*1%");
			chenhao.setName(myGang.getName() + " "
					+ MmochatChenHaoType.帮众.toString());
			MmochatChenHao.addAndUpdateChenHaoAndSaveSql(hisInfo.getRoleId(),
					chenhao);

			break;
		}
		default:
			break;
		}

		// 更新帮众列表
		Mmochat_15_4S pack = new Mmochat_15_4S();
		pack.setHallState(req.getHallState());
		pack.setGang(myGang);
		pack.setCurPage(req.getPage());
		return pack;
	}

	// 对单人审批
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_8(SkymobiHandler handler,
			Mmochat_15_8C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您不在此帮派!");
		}
		if (myInfo.getType() != MmochatGangMemberType.帮主
				&& myInfo.getType() != MmochatGangMemberType.副帮) {
			return MmochatUtil.msgbox(req.getHallState(), "您没有权限审批!");
		}
		MmochatGangRequester requester = null;
		if (myGang.getRequesters().containsKey(req.getTargetRoleId())) {
			synchronized (myGang) {
				// 尚未审批
				if (myGang.getRequesters().containsKey(req.getTargetRoleId())) {
					requester = myGang.getRequesters().get(
							req.getTargetRoleId());
					myGang.getRequesters().remove(req.getTargetRoleId());
				}
			}
		}
		if (requester != null) {
			if (req.getDealResult() == 0) {
				// 拒绝
				// 向此玩家发出通知
				MmochatPlayer he = MmochatMainService.players.get(requester
						.getRoleId());
				if (he != null) {
					if (!he.hasGang()) {
						MmochatFriendService
								.sendSystemMsgToPersonByFriendMessage(he,
										myGang.getName() + "帮派拒绝了您的入帮请求。");
					}
				}
			} else {
				// 同意
				MmochatPlayer he = MmochatMainService.players.get(requester
						.getRoleId());
				if (he == null) {
					return MmochatUtil.msgbox(req.getHallState(),
							"对方不在线，无法加其为成员!");
				}
				if (myGang.isMemberFull()) {
					return MmochatUtil.msgbox(req.getHallState(),
							"帮派人员已达上限，无法再增加成员!");
				}

				MmochatGangMember newMember = new MmochatGangMember();
				newMember.setBmpId(requester.getBmpId());
				newMember.setBuildValue(0);
				newMember.setCurWeekBuildValue(0);
				newMember.setName(requester.getName());
				newMember.setRoleId(requester.getRoleId());
				newMember.setType(MmochatGangMemberType.帮众);

				synchronized (he) {
					if (he.hasGang()) {
						return MmochatUtil.msgbox(req.getHallState(),
								"对方已经有帮派了!");
					}
					he.setGangId(myGang.getId());
				}

				synchronized (myGang) {
					if (!myGang.isMemberFull()) {
						if (myGang.getMembers().get(requester.getRoleId()) == null) {
							myGang.getMembers().put(newMember.getRoleId(),
									newMember);
							try {
								MmochatDao.addGangMember(newMember.getRoleId(),
										myGang);
							} catch (Exception e) {
								e.printStackTrace();
								myGang.getMembers().remove(
										requester.getRoleId());
								return MmochatUtil.msgbox(req.getHallState(),
										"服务器忙,请稍候再试!");
							}
						}
					} else {
						if (myGang.getMembers().get(requester.getRoleId()) == null) {
							he.setNoGang();
						}
					}
				}
				// 称号处理
				MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
						.getGangChenhaoList();
				for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
					MmochatChenHao.delAndUpdateChenHaoAndSaveSql(requester
							.getRoleId(), chenhaoType);
				}

				MmochatChenHao chenhao = new MmochatChenHao();
				chenhao.setType(MmochatChenHaoType.帮众);
				chenhao.setExtraEffectInfo("同气连枝:与同帮派玩家组队时，防御增加同帮人数*1%");
				chenhao.setName(myGang.getName() + " "
						+ MmochatChenHaoType.帮众.toString());
				MmochatChenHao.addAndUpdateChenHaoAndSaveSql(requester
						.getRoleId(), chenhao);

				// 通知此玩家,并通过帮派频道向所有成员发布消息
				if (he != null) {
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							he, "恭喜!您加入了帮派:" + myGang.getName() + "。");
				}
				sendGangSystemMsg(myGang, MmochatUtil.wrapColor(requester
						.getName(), Color.green)
						+ "加入了帮派。");
			}
		}
		Mmochat_15_10S pack = new Mmochat_15_10S();
		pack.setHallState(req.getHallState());
		pack.setGang(myGang);
		return pack;
	}

	// 对所有人同时审批
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_9(SkymobiHandler handler,
			Mmochat_15_9C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您不在此帮派!");
		}
		if (myInfo.getType() != MmochatGangMemberType.帮主
				&& myInfo.getType() != MmochatGangMemberType.副帮) {
			return MmochatUtil.msgbox(req.getHallState(), "您没有权限审批!");
		}

		for (MmochatGangRequester requester : myGang.getRequesters().values()) {
			if (req.getDealResult() == 0) {
				// 拒绝
				// 向此玩家发出通知
				MmochatPlayer he = MmochatMainService.players.get(requester
						.getRoleId());
				if (he != null) {
					MmochatFriendService.sendSystemMsgToPersonByFriendMessage(
							he, myGang.getName() + "帮派拒绝了您的入帮请求。");
				}
			}
		}
		myGang.getRequesters().clear();
		return null;
	}

	// 退出帮派
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_11(SkymobiHandler handler,
			Mmochat_15_11C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang myGang = getGangById(me.getGangId());
		if (myGang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember myInfo = myGang.getMembers().get(me.getRoleId());
		if (myInfo == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		// 退出帮派
		if (myInfo.getType() == MmochatGangMemberType.帮主) {
			// 解散帮派
			if (myGang.getMembers().size() > 1) {
				return MmochatUtil.msgbox(req.getHallState(),
						"您的帮派尚有其它成员，作为帮主，您不能就此退出帮派。如果您坚持要退出，"
								+ "您可以将帮主之位传给其它成员，或者当帮内没有其它成员时方可退出。");
			} else {
				// 删除帮派任务
				MmochatTask myTask = me.getTaskByType(MmochatTaskType.帮派任务);
				if (myTask != null
						&& (myTask.getTaskState() == MmochatTaskStateType.已接 || myTask
								.getTaskState() == MmochatTaskStateType.待完成)) {
					me.getTasks().remove(myTask.getTaskId());
					Integer nextTaskId = myTask.getNext_taskId();
					while (nextTaskId != null) {
						MmochatTask task = me.getTaskById(nextTaskId);
						if (task == null) {
							break;
						}
						me.getTasks().remove(nextTaskId);
						nextTaskId = task.getNext_taskId();
					}
					Integer nextAccept_taskId = myTask.getNextAccept_taskId();
					while (nextAccept_taskId != null) {
						MmochatTask task = me.getTaskById(nextAccept_taskId);
						if (task == null) {
							break;
						}
						me.getTasks().remove(nextAccept_taskId);
						nextAccept_taskId = task.getNextAccept_taskId();

						// 下发删除任务
						Mmochat_4_12S taskPack = new Mmochat_4_12S();
						taskPack.setTaskId(nextAccept_taskId);
						MmochatUtil.sendCommonPack(me, taskPack);
					}

					try {
						MmochatDao.updateRoleTasks(me);
					} catch (Exception e) {
						e.printStackTrace();
						return MmochatUtil.msgbox(req.getHallState(),
								"服务器忙,请稍候再试!");
					}
				}

				// 保存数据库,更新帮派和被踢者的帮派信息，如果在线，还要更新其内存中的帮派信息
				try {
					MmochatDao.dismissGang(myGang, me);
					MmochatGangService.gangs.remove(myGang.getId());
					MmochatMapService.maps.remove(myGang.getMapId());
				} catch (Exception e) {
					e.printStackTrace();
					// 失败
					return MmochatUtil
							.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
				}
			}
		} else {
			// 成员退出
			// 删除帮派任务
			MmochatTask myTask = me.getTaskByType(MmochatTaskType.帮派任务);
			if (myTask != null
					&& (myTask.getTaskState() == MmochatTaskStateType.已接 || myTask
							.getTaskState() == MmochatTaskStateType.待完成)) {
				me.getTasks().remove(myTask.getTaskId());
				Integer nextTaskId = myTask.getNext_taskId();
				while (nextTaskId != null) {
					MmochatTask task = me.getTaskById(nextTaskId);
					if (task == null) {
						break;
					}
					me.getTasks().remove(nextTaskId);
					nextTaskId = task.getNext_taskId();

					// 下发删除任务
					Mmochat_4_12S taskPack = new Mmochat_4_12S();
					taskPack.setTaskId(nextTaskId);
					MmochatUtil.sendCommonPack(me, taskPack);
				}
				Integer nextAccept_taskId = myTask.getNextAccept_taskId();
				while (nextAccept_taskId != null) {
					MmochatTask task = me.getTaskById(nextAccept_taskId);
					if (task == null) {
						break;
					}
					me.getTasks().remove(nextAccept_taskId);
					nextAccept_taskId = task.getNextAccept_taskId();
				}
			}

			myGang.getMembers().remove(me.getRoleId());
			// 保存数据库,更新帮派和被踢者的帮派信息，如果在线，还要更新其内存中的帮派信息
			try {
				MmochatDao.kickOutGangMember(myGang, me.getRoleId(), me);
				// 向帮众发布消息
				sendGangSystemMsg(myGang, MmochatUtil.wrapColor(me.getName(),
						Color.yellow)
						+ "离开了帮派。");
			} catch (Exception e) {
				e.printStackTrace();
				// 踢出失败
				myGang.getMembers().put(me.getRoleId(), myInfo);
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
			}
		}
		// 称号处理
		MmochatChenHaoType[] gangChenhaoList = MmochatChenHaoType
				.getGangChenhaoList();
		for (MmochatChenHaoType chenhaoType : gangChenhaoList) {
			MmochatChenHao.delAndUpdateChenHaoAndSaveSql(me.getRoleId(),
					chenhaoType);
		}

		return MmochatUtil.msgbox(req.getHallState(), "您已经退出该帮派!");
	}

	// 申请加入指定帮派
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_13(SkymobiHandler handler,
			Mmochat_15_13C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您已经加入了帮派!");
		}

		MmochatGang gang = getGangById(req.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此帮派不存在!");
		}
		if (gang.getRequesters().containsKey(me.getRoleId())) {
			return MmochatUtil.msgbox(req.getHallState(),
					"您已经向此帮派发出过入帮申请了，请等待帮派管理员的审批!");
		}
		if (gang.isRequestFull()) {
			return MmochatUtil.msgbox(req.getHallState(),
					"申请此帮派的人数太多,暂时不接受新的申请!");
		}
		MmochatGangRequester requester = new MmochatGangRequester();
		requester.setBmpId(me.getBmpId());
		requester.setLevel(me.getLevel());
		requester.setName(me.getName());
		requester.setRoleId(me.getRoleId());
		gang.getRequesters().put(me.getRoleId(), requester);

		// 向帮派管理员下发闪动提示
		for (MmochatGangMember myInfo : gang.getMembers().values()) {
			if (myInfo.getType() == MmochatGangMemberType.帮主
					|| myInfo.getType() == MmochatGangMemberType.副帮) {
				MmochatPlayer member = MmochatMainService.players.get(myInfo
						.getRoleId());
				if (member != null) {
					Mmochat_4_8S pack = new Mmochat_4_8S();
					pack.setHintKey(MmochatConstant.MR_KEY_8);
					pack.setFlashTimes(6);
					MmochatUtil.sendCommonPack(member, pack);
				}
			}
		}
		return MmochatUtil.msgbox(req.getHallState(), "入帮申请已发出，请耐心等待帮派管理员的审核!");
	}

	// 查看指定页的帮派列表
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_14(SkymobiHandler handler,
			Mmochat_15_14C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		int page = req.getPage();
		Mmochat_15_12S pack = new Mmochat_15_12S();
		pack.setCurPage(page);
		pack.setGangList(getAllGang());
		pack.setHallState(req.getHallState());

		return pack;
	}

	// 进入帮派大乱斗场地
	@SuppressWarnings("deprecation")
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_15(SkymobiHandler handler,
			Mmochat_15_15C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		if (me.getLine() != MmochatGangMixedPkGame.pkLine) {
			return MmochatUtil.msgbox(req.getHallState(), "帮派大乱斗定在"
					+ (MmochatGangMixedPkGame.pkLine + 1) + "线举行,请换线。");
		}
		// 每周二、五20点-22点开放, 19:40-20:00入场
		Date now = new Date();
		if ((now.getDay() != 2 && now.getDay() != 5) || now.getHours() != 19
				|| now.getMinutes() < 40) {
			return MmochatUtil.msgbox(req.getHallState(),
					"帮派大乱斗入场时间:每周二、周五19:40-20:00,现在不是入场时间!");
		}

		// 进入场地
		int targetMapId = MmochatConstant.mapId_gangMixedBattle;
		MmochatMap newMap = MmochatMapService.getMapByMapId(targetMapId);
		if (newMap == null) {
			return MmochatUtil.msgbox(req.getHallState(), "会场尚未准备好,请稍候再来!");
		}

		// 地图传送目的地
		int targetX = 235, targetY = 290;
		if (me.getMapId() == targetMapId) {
			log.debug("玩家" + me.getName() + "(skyid=" + me.getSkyId()
					+ ")已经在mapId=" + me.getMapId() + "的地图了!,无需传送");
			return null;
		}
		if (me.isInBattle()) {
			log.debug("玩家" + me.getName() + "(skyid=" + me.getSkyId()
					+ ")已进入战斗,无法传送");
			return null;
		}
		List<MmochatPlayer> members = new ArrayList<MmochatPlayer>();
		if (me.isInTeam()) {
			MmochatTeam team = MmochatTeamService.teams.get(me.getTeamId());
			if (team == null) {
				me.leaveTeam();
				members.add(me);
			} else {
				if (!team.isTeamLeader(me.getRoleId())) {
					log.debug("玩家" + me.getName() + "(skyid=" + me.getSkyId()
							+ ")在队伍中，且不为队长，无法传送;");
					return MmochatUtil.msgbox(req.getHallState(),
							"您在队伍中，请和队长一起行动!");
				}
				for (Integer roleId : team.getMemberId().values()) {
					MmochatPlayer member = MmochatMainService.players
							.get(roleId);
					if (member != null) {
						members.add(member);
					}
				}
			}
		} else {
			members.add(me);
		}
		if (members.size() <= 0) {
			log.debug("玩家" + me.getName() + "(skyid=" + me.getSkyId()
					+ ")传送人数为0，无法传送;");
			return null;
		}
		for (MmochatPlayer player : members) {
			if (player.getGangId() != me.getGangId()) {
				return MmochatUtil.msgbox(req.getHallState(), player.getName()
						+ "不是本帮成员，无法进入!");
			}
		}
		// 通过x,y决定传送到哪个地图哪个传送口附近
		MmochatMap curMap = MmochatMapService.getMapByMapId(me.getMapId());

		// 将本队人马传到新地图新坐标
		MmochatMapService.tranToNewMap(curMap, me.getLine(), newMap, me
				.getLine(), targetX, targetY, members);
		log.debug("玩家" + me.getName() + "(skyid=" + me.getSkyId() + ")完成传送");
		return null;
	}

	// 查看帮派技能信息
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_16(SkymobiHandler handler,
			Mmochat_15_16C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangSkillType skillType = MmochatGangSkillType.get(req
				.getSkillId());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能不存在!");
		}
		int skillLevel = 0;
		MmochatGangSkill skill = gang.getSkills().get(skillType);
		if (skill != null) {
			skillLevel = skill.getLevel();
		}

		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.Text);
		tlv.setRightName("返回");
		tlv.setTitle(skillType.toString());
		tlv.setWinHeight(208);
		tlv.addRightTLV(new CloseWinTLV());

		String info;
		info = MmochatUtil.wrapColor("[技能效果]", Color.yellow);
		info += "\n" + skillType.getIntro();
		info += "\n\n" + MmochatUtil.wrapColor("[当前效果]", Color.yellow);
		if (skillLevel == 0) {
			info += "\n无";
		} else {
			int effect = skillType.getGangSkillEffectPercent(skillLevel);
			info += "\n" + skillType.getIntro() + effect + "%";
		}
		info += "\n\n" + MmochatUtil.wrapColor("[下阶效果]", Color.yellow);

		if (skillLevel >= skillType.getMaxLevel()) {
			info += "\n无";
		} else {
			int effect = skillType.getGangSkillEffectPercent(skillLevel + 1);
			info += "\n" + skillType.getIntro() + effect + "%";
		}
		tlv.setContent(new Text(info));
		return MmochatUtil.tlvResponse(req.getHallState(), tlv);
	}

	// 研发帮派技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_17(SkymobiHandler handler,
			Mmochat_15_17C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember member = gang.getMembers().get(me.getRoleId());
		if (member == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		if (member.getType() != MmochatGangMemberType.帮主) {
			return MmochatUtil
					.msgbox(req.getHallState(), "只有帮主才能研发帮派技能,您不是帮主!");
		}

		MmochatGangSkillType skillType = MmochatGangSkillType.get(req
				.getSkillId());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能不存在!");
		}

		int skillLevel = 0;
		MmochatGangSkill skill = gang.getSkills().get(skillType);
		if (skill != null) {
			skillLevel = skill.getLevel();
		}
		if (skillLevel >= skillType.getMaxLevel()) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能贵帮已尽得真传!");
		}

		MmochatGangLevelType gangLevelNeed = skillType
				.getGangLevelNeedToDevelopSkill(skillLevel);
		if (gang.getLevel().getValue() < gangLevelNeed.getValue()) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能只有"
					+ gangLevelNeed.toString() + "以上帮派才能研发。");
		}
		// 需要建设度
		int buildNeed = skillType.getGangBuildNeedToDevelopSkill(skillLevel);

		// 可用建设度
		int buildCanUse = gang.getBuildValue() - gang.getLevel().getBuildNeed();

		if (buildCanUse < buildNeed) {
			return MmochatUtil.msgbox(req.getHallState(), "贵帮当前可用建设度为"
					+ buildCanUse + "点，研发此技能需要" + buildNeed
					+ "点建设度。建设度不足，无法研发!");
		}

		String msg = "研发"
				+ MmochatUtil.wrapColor(skillType.toString(), Color.yellow)
				+ "第" + (skillLevel + 1) + "级技能需要消耗"
				+ MmochatUtil.wrapColor(buildNeed + "", Color.green)
				+ "点帮派建设度,贵帮当前可用建设度为"
				+ MmochatUtil.wrapColor(buildCanUse + "", Color.green)
				+ "点,是否确认研发?";
		CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
		msgbox.setContent(new Text(msg));
		msgbox.setLeftName("研发");
		msgbox.setRightName("取消");
		msgbox.addLeftTLV(new ClearDataTLV());
		msgbox.addLeftTLV(new AddDataTLV((int) me.getRoleId()));
		msgbox
				.addLeftTLV(new SendDataTLV(
						MmochatConstant.getClientModuleId(), MmochatMain
								.getModuleInstID(),
						MmochatConstant.MSGCODE_MMOCHAT_C_15_18, (int) req
								.getSkillId()));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		msgbox.addLeftTLV(wait);
		return MmochatUtil.tlvResponse(req.getHallState(), msgbox);
	}

	// 确认研发技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_18(SkymobiHandler handler,
			Mmochat_15_18C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember member = gang.getMembers().get(me.getRoleId());
		if (member == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		if (member.getType() != MmochatGangMemberType.帮主) {
			return MmochatUtil
					.msgbox(req.getHallState(), "只有帮主才能研发帮派技能,您不是帮主!");
		}

		MmochatGangSkillType skillType = MmochatGangSkillType.get(req
				.getSkillId());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能不存在!");
		}

		int skillLevel = 0;
		MmochatGangSkill skill = gang.getSkills().get(skillType);
		if (skill != null) {
			skillLevel = skill.getLevel();
		}
		if (skillLevel >= skillType.getMaxLevel()) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能贵帮已尽得真传!");
		}

		MmochatGangLevelType gangLevelNeed = skillType
				.getGangLevelNeedToDevelopSkill(skillLevel);
		if (gang.getLevel().getValue() < gangLevelNeed.getValue()) {
			return MmochatUtil.msgbox(req.getHallState(), "此技能只有"
					+ gangLevelNeed.toString() + "以上帮派才能研发。");
		}
		// 需要建设度
		int buildNeed = skillType.getGangBuildNeedToDevelopSkill(skillLevel);

		// 可用建设度
		int buildCanUse = gang.getBuildValue() - gang.getLevel().getBuildNeed();

		if (buildCanUse < buildNeed) {
			return MmochatUtil.msgbox(req.getHallState(), "贵帮当前可用建设度为"
					+ buildCanUse + "点，研发此技能需要" + buildNeed
					+ "点建设度。建设度不足，无法研发!");
		}

		// 研发技能：扣除建设度，增加技能等级，保存数据库
		gang.addBuildValue(-1 * buildNeed);
		if (skill == null) {
			// 尚未学技能,先学习技能
			MmochatGangSkill newSkill = new MmochatGangSkill();
			newSkill.setType(skillType);
			newSkill.setLevel(0);
			gang.getSkills().put(skillType, newSkill);
			skill = newSkill;
		}
		skill.addLevel();
		try {
			MmochatDao.developGangSkill(gang, buildNeed);
		} catch (Exception e) {
			e.printStackTrace();
			skill.desLevel();
			gang.addBuildValue(buildNeed);
			return MmochatUtil.msgbox(req.getHallState(), "数据库忙，请稍候再试!");
		}
		Mmochat_15_2S pack = new Mmochat_15_2S();
		pack.setHallState(req.getHallState());
		pack.setGang(gang);
		pack.setMyType(member.getType());
		return pack;
	}

	// 成员学习帮派技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_19(SkymobiHandler handler,
			Mmochat_15_19C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember member = gang.getMembers().get(me.getRoleId());
		if (member == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangSkillType skillType = MmochatGangSkillType.get(req
				.getSkillId());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此技能!");
		}

		int skillLevel = 0;
		MmochatGangSkill skill = gang.getSkills().get(skillType);
		if (skill != null) {
			skillLevel = skill.getLevel();
		}
		if (skillLevel == 0) {
			return MmochatUtil.msgbox(req.getHallState(), "本帮尚未研发出此技能!");
		}

		int mySkillLevel = 0;
		MmochatGangSkill mySkill = me.getGangSkills().get(skillType);
		if (mySkill != null) {
			mySkillLevel = mySkill.getLevel();
		}

		if (mySkillLevel >= skillLevel) {
			return MmochatUtil.msgbox(req.getHallState(), "您当前此技能已习得第"
					+ mySkillLevel + "级,本帮尚未研出发更高阶的技能。");
		}

		// 可用贡献值
		int buildCanUse = member.getCanUseBuildValue();

		// 需要贡献
		int buildNeed = skillType
				.getGangBuildNeedToLearnSkill(mySkillLevel + 1);

		if (buildCanUse < buildNeed) {
			return MmochatUtil.msgbox(req.getHallState(), "学习"
					+ MmochatUtil.wrapColor(skillType.toString(), Color.yellow)
					+ "第" + (mySkillLevel + 1) + "级需要花费"
					+ MmochatUtil.wrapColor(buildNeed + "", Color.green)
					+ "点帮派贡献，您当前可用的贡献值为"
					+ MmochatUtil.wrapColor(buildCanUse + "", Color.green)
					+ ",您的可用贡献不足。");
		}
		String msg = "学习"
				+ MmochatUtil.wrapColor(skillType.toString(), Color.yellow)
				+ "第" + (mySkillLevel + 1) + "级需要花费"
				+ MmochatUtil.wrapColor(buildNeed + "", Color.green)
				+ "点帮派贡献，您当前可用的贡献值为"
				+ MmochatUtil.wrapColor(buildCanUse + "", Color.green)
				+ ",是否学习?";
		CreateMsgBoxTLV msgbox = new CreateMsgBoxTLV();
		msgbox.setContent(new Text(msg));
		msgbox.setLeftName("学习");
		msgbox.setRightName("取消");
		msgbox.addLeftTLV(new ClearDataTLV());
		msgbox.addLeftTLV(new AddDataTLV((int) me.getRoleId()));
		msgbox
				.addLeftTLV(new SendDataTLV(
						MmochatConstant.getClientModuleId(), MmochatMain
								.getModuleInstID(),
						MmochatConstant.MSGCODE_MMOCHAT_C_15_20, (int) req
								.getSkillId()));
		ShowWaitingMsgBoxTLV wait = new ShowWaitingMsgBoxTLV();
		wait.setShowCancel(false);
		msgbox.addLeftTLV(wait);
		return MmochatUtil.tlvResponse(req.getHallState(), msgbox);
	}

	// 确认学习技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_20(SkymobiHandler handler,
			Mmochat_15_20C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember member = gang.getMembers().get(me.getRoleId());
		if (member == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangSkillType skillType = MmochatGangSkillType.get(req
				.getSkillId());
		if (skillType == null) {
			return MmochatUtil.msgbox(req.getHallState(), "无此技能!");
		}

		int skillLevel = 0;
		MmochatGangSkill skill = gang.getSkills().get(skillType);
		if (skill != null) {
			skillLevel = skill.getLevel();
		}
		if (skillLevel == 0) {
			return MmochatUtil.msgbox(req.getHallState(), "本帮尚未研发出此技能!");
		}

		int mySkillLevel = 0;
		MmochatGangSkill mySkill = me.getGangSkills().get(skillType);
		if (mySkill != null) {
			mySkillLevel = mySkill.getLevel();
		}

		if (mySkillLevel >= skillLevel) {
			return MmochatUtil.msgbox(req.getHallState(), "您当前此技能已习得第"
					+ mySkillLevel + "级,本帮尚未研出发更高阶的技能。");
		}

		// 可用贡献值
		int buildCanUse = member.getCanUseBuildValue();

		// 需要贡献
		int buildNeed = skillType
				.getGangBuildNeedToLearnSkill(mySkillLevel + 1);

		if (buildCanUse < buildNeed) {
			return MmochatUtil.msgbox(req.getHallState(), "学习"
					+ MmochatUtil.wrapColor(skillType.toString(), Color.yellow)
					+ "第" + (mySkillLevel + 1) + "级需要花费"
					+ MmochatUtil.wrapColor(buildNeed + "", Color.green)
					+ "点帮派贡献，您当前可用的贡献值为"
					+ MmochatUtil.wrapColor(buildCanUse + "", Color.green)
					+ ",您的可用贡献不足。");
		}

		// 学习技能：扣除可用贡献，增加技能等级，保存数据库
		member.delCanUseBuildValue(buildNeed);
		if (mySkill == null) {
			// 第一次学习此技能
			mySkill = new MmochatGangSkill();
			me.getGangSkills().put(skillType, mySkill);
		}
		mySkill.setLevel(mySkillLevel + 1);
		try {
			MmochatDao.learnGangSkill(gang, me);
		} catch (Exception e) {
			e.printStackTrace();
			member.delCanUseBuildValue(-1 * buildNeed);
			mySkill.setLevel(mySkillLevel);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		return MmochatUtil.msgbox(req.getHallState(), "学习成功!您当前"
				+ MmochatUtil.wrapColor(skillType.toString(), Color.yellow)
				+ "已提升到"
				+ MmochatUtil.wrapColor(mySkill.getLevel() + "", Color.green)
				+ "级。剩余可用贡献值为" + member.getCanUseBuildValue() + "。");
	}

	// 确认升级帮派
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_21(SkymobiHandler handler,
			Mmochat_15_21C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember member = gang.getMembers().get(me.getRoleId());
		if (member == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		if (member.getType() != MmochatGangMemberType.帮主) {
			return MmochatUtil.msgbox(req.getHallState(), "只有帮主才能升级帮派,您不是帮主!");
		}

		MmochatGangLevelType gangLevel = gang.getLevel();
		MmochatGangLevelType maxLevel = MmochatGangLevelType.getMaxLevelType();
		if (gangLevel.getValue() == maxLevel.getValue()) {
			return MmochatUtil
					.msgbox(req.getHallState(), "本帮已经升至最高级别帮派,无法再升级!");
		}
		MmochatGangLevelType nextLevel = MmochatGangLevelType
				.getNextLevelType(gangLevel);
		int needBuild = nextLevel.getBuildNeed();
		if (gang.getBuildValue() < needBuild) {
			return MmochatUtil.msgbox(req.getHallState(), "升级至"
					+ nextLevel.toString() + "帮派,要求帮派建设度达到"
					+ MmochatUtil.wrapColor(needBuild + "", Color.yellow)
					+ ",本帮建设度尚未达标。");
		}

		// 升级帮派:提升帮派等级，保存数据库
		gang.setLevel(nextLevel);
		try {
			MmochatDao.updateGangLevel(gang);
		} catch (Exception e) {
			e.printStackTrace();
			gang.setLevel(gangLevel);
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		return MmochatUtil.msgbox(req.getHallState(), "升级成功!本帮已升至"
				+ MmochatUtil.wrapColor(gang.getLevel().toString() + "帮派",
						Color.yellow));
	}

	// 查看我的帮派技能
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_22(SkymobiHandler handler,
			Mmochat_15_22C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}

		CreateTlvWin tlv = new CreateTlvWin();
		tlv.setWinType(WinType.List);
		tlv.setRightName("返回");
		tlv.setTitle("我的帮派技能");
		tlv.setWinHeight(228);
		tlv.addRightTLV(new CloseWinTLV());

		for (MmochatGangSkillType skillType : MmochatGangSkillType.values()) {
			MmochatGangSkill skill = me.getGangSkills().get(skillType);
			int skillLevel = 0;
			if (skill != null) {
				skillLevel = skill.getLevel();
			}
			String item = skillType.toString() + "(" + skillLevel + "级)";
			item += "\n";
			item += MmochatUtil.wrapColor(skillType.getIntro()
					+ skillType.getGangSkillEffectPercent(skillLevel) + "%",
					Color.green);
			tlv.addItemEvent(item, new ClearDataTLV(), new AddDataTLV(
					(int) skillType.getValue()));
		}
		return MmochatUtil.tlvResponse(req.getHallState(), tlv);
	}

	// 提取帮派资金
	@SkymobiService
	public SkymobiProtocolMessage handleC_15_23(SkymobiHandler handler,
			Mmochat_15_23C req) {
		MmochatPlayer me = MmochatMainService.getPlayerByRoleId(req, req
				.getRoleId());
		if (me == null) {
			return MmochatUtil.msgbox(req.getHallState(), "服务器忙,请稍候再试!");
		}
		if (!me.hasGang()) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGang gang = getGangById(me.getGangId());
		if (gang == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		MmochatGangMember member = gang.getMembers().get(me.getRoleId());
		if (member == null) {
			return MmochatUtil.msgbox(req.getHallState(), "您还没有加入帮派!");
		}
		if (member.getType() != MmochatGangMemberType.帮主) {
			return MmochatUtil.msgbox(req.getHallState(), "只有帮主才能提取帮派资金!");
		}

		String moneyStr = req.getMoneyStr();
		Integer money = 0;
		try {
			money = Integer.parseInt(moneyStr);
		} catch (NumberFormatException e) {
			return MmochatUtil.msgbox(req.getHallState(), "只能输入大于0的数字!");
		}
		if (money <= 0) {
			return MmochatUtil.msgbox(req.getHallState(), "只能输入大于0的数字!");
		}
		synchronized (gang) {
			money = Math.min(money, gang.getMoney());
			if (money <= 0) {
				return MmochatUtil.msgbox(req.getHallState(), "您的帮派资金不足!");
			}
			// 扣除帮派资金，增加到帮主身上
			try {
				MmochatDao.delGangMoneyToManage(money, gang.getId(), me
						.getRoleId());
			} catch (Exception e) {
				e.printStackTrace();
				return MmochatUtil.msgbox(req.getHallState(), "服务器忙，请稍候再试!");
			}
			me.addAndUpdateSmall_money(money);
			gang.addMoney(-1 * money);
		}
		MmochatStatisticsService.addSmallMoneyEarn(
				MmochatSmallMoneyEarnType.帮派资金提取, money);
		return MmochatUtil.msgbox(req.getHallState(), "您从帮派提取了"
				+ MmochatUtil.getColorMoney(money) + "文钱!");
	}
}
